package forms;

import org.apache.struts.action.*;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import java.sql.*;

public class appRegForm extends ActionForm {

	private static final long serialVersionUID = 1L;

	private String username = null;
	private String pwd = null;
	private String rpwd = null;
	private String email = null;
	private boolean uErr = false;
	private boolean eErr = false;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getRpwd() {
		return rpwd;
	}

	public void setRpwd(String rpwd) {
		this.rpwd = rpwd;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void reset(ActionMapping mapping, HttpServletRequest request) {
		setUsername("");
		setPwd("");
		setRpwd("");
		setEmail("");
	}

	// check if first name, middle initial, and last name are provided and valid
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();

		String url = "jdbc:postgresql://localhost:5432/Applications";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;

		try {
			// Registering Postgresql JDBC driver
			Class.forName("org.postgresql.Driver");
			// Open a connection to the database
			conn = DriverManager.getConnection(url, "postgres", "postgres");
			// Create the statement
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_UPDATABLE);

			if (username.equals("") && pwd.equals("") && email.equals("")) {
				errors.add("username", new ActionMessage(
						"error.username.required"));
				setUsername("");
				errors.add("pwd", new ActionMessage("error.pwd.required"));
				setPwd("");
				errors.add("email", new ActionMessage("error.email.required"));
				setEmail("");
			} else {
				if (username.equals("")) {
					errors.add("username", new ActionMessage(
							"error.username.required"));
					setUsername("");
				} else {
					// make sure the username hasn't already been used
					rs = stmt.executeQuery("SELECT user_name FROM users");
					while (rs.next()) {
						if (username.equals(rs.getString("user_name"))) {
							uErr = true;
						}
					}
				}
				if (pwd.equals("")) {
					errors.add("pwd", new ActionMessage("error.pwd.required"));
					setPwd("");
				} else {
					if (!(pwd.equals(rpwd))) {
						errors.add("pwd", new ActionMessage(
								"error.pwd.mismatch"));
						setPwd("");
						setRpwd("");
					}
				}
				if (email.equals("")) {
					errors.add("email", new ActionMessage(
							"error.email.required"));
					setEmail("");
				} else {
					// make sure this user hasn't registered already (email
					// exists)
					rs = stmt.executeQuery("SELECT Email FROM users");
					while (rs.next()) {
						if (email.equals(rs.getString("Email"))) {
							eErr = true;
						}
					}
				}

				if (uErr) {
					errors.add("username", new ActionMessage(
							"error.username.duplicate"));
					uErr = false;
					setUsername("");
				}
				if (eErr) {
					errors.add("email", new ActionMessage(
							"error.email.duplicate"));
					eErr = false;
					setEmail("");
				}
			}

			// close connection
			if (rs != null) {
				rs.close();
			}
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			System.out.println(e);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return errors;
	}
}
